package com.bjsxt.userscenter.common.util;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;

@Controller("springEmailUtil")
public class SpringEmailUtil
{

	@Resource(name="javaMailSenderImpl")
	private JavaMailSender javaMailSender;
	
	@Resource
	private SimpleMailMessage simpleMailMessage;
	
	public boolean sendMail(String to, String subject,String text)
	{
		try
		{
			simpleMailMessage.setTo(to);
			simpleMailMessage.setSubject(subject);
			simpleMailMessage.setText(text);
			
			this.javaMailSender.send(this.simpleMailMessage);
			return true;
		} catch (MailException e)
		{
		
			ConstatFinalUtil.LOGGER.error("发送失败发送给:{},主题:{},文本内容:{}",to,subject,text,e);
		}
		
		return false;
	}
	/**
	 * 发送html
	 * @return
	 */
	public boolean sendMailHtml(String to, String subject,String text)
	{
		try
		{
			MimeMessage message = this.javaMailSender.createMimeMessage();
			MimeMessageHelper hpler = new MimeMessageHelper(message,true,"Utf-8");
			/**
			 * 设置里面的内容
			 */
			hpler.setFrom(this.simpleMailMessage.getFrom());
			hpler.setTo(to);
			hpler.setSubject(subject);
			hpler.setText(text);
			javaMailSender.send(message);
			return true;
			
		} catch (MessagingException e)
		{
			ConstatFinalUtil.LOGGER.info("发送失败,to:{},subject:{},text:{}",to,subject,text,e);
		}
		return false;
	}
	
}
